#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <stdarg.h> 

#include "data_type.h"
#include "alloc.h"
#include "json.h"
#include "memfunc.h"
#include "struct_deal.h"
#include "crypto_func.h"
#include "basefunc.h"
#include "memdb.h"
#include "message.h"
#include "connector.h"
#include "ex_module.h"
#include "errno.h"
#include "sys_func.h"


int read_sys_cfg(void ** lib_para_struct,void * root_node,char * plugin_dir)
{
    int json_offset;	
    int ret;
    int i,j;
    int len;	
    char filename[DIGEST_SIZE*8];
    char * define_path[10];
    char * define_path_env;
    char * define_buf;
	
    void * struct_template=memdb_get_template(DTYPE_EXMODULE,SUBTYPE_LIB_PARA);
    if(struct_template==NULL)
    {
	print_cubeerr("Fatal error!,wrong lib para format!\n");
	return -CUBEERR_INVAL;
    }
 
    define_path_env=getenv("CUBE_DEFINE_PATH");	
    define_buf=Talloc0(Strlen(define_path_env)+1);
    if(define_buf==NULL)
	return -CUBEERR_NOMEM;    	
    Strcpy(define_buf,define_path_env);
    	
    j=0;
    define_path[0]=define_buf;
    define_path[1]=NULL;	
   
    len=Strlen(define_buf);	
    for(i=0;i<len;i++)
    {
	if(define_buf[i]==':')
	{
		define_buf[i]='\0';
		if(define_buf[i+1]!='\0')
		{
			j++;
			define_path[j]=define_buf+i+1;
			define_path[j+1]=NULL;
		}
	}
    }
    	

    void * define_node=json_find_elem("define_file",root_node);	    
    if(define_node!=NULL)
    {
	if(json_get_type(define_node)==JSON_ELEM_STRING)
	{
		ret=read_json_file(json_get_valuestr(define_node));
		if(ret<0)
		{
			j=0;
			while(define_path[j]!=NULL)
			{
				Strcpy(filename,define_path[j]);
				Strcat(filename,"/");
				Strcat(filename,json_get_valuestr(define_node));					
				ret=read_json_file(filename);
				if(ret>=0)
				{
					break;
				}
				j++;
			}
			if(define_path[j]==NULL)
				print_cubeerr("read define file  %s failed!",json_get_valuestr(define_node));
		}
		if(ret>=0)
			print_cubeaudit("read %d elem from file %s!",ret,json_get_valuestr(define_node));
	}
	else if(json_get_type(define_node)==JSON_ELEM_ARRAY)
	{
		void * define_file=json_get_first_child(define_node);
		while(define_file!=NULL)
		{
	        	ret=read_json_file(json_get_valuestr(define_file));
			if(ret<0)
			{
				j=0;
				while(define_path[j]!=NULL)
				{
					Strcpy(filename,define_path[j]);
					Strcat(filename,"/");
					Strcat(filename,json_get_valuestr(define_file));					
					ret=read_json_file(filename);
					if(ret>0)
					{
						break;
					}
					j++;
				}
				if(define_path[j]==NULL)
				{
					print_cubeerr("read define file  %s failed!",json_get_valuestr(define_file));
				}
			}	
			if(ret>=0)
				print_cubeaudit("read %d elem from file %s!",ret,json_get_valuestr(define_file));
			define_file=json_get_next_child(define_node);
		}
	}	
    }

    void * para_node=json_find_elem("init_para_desc",root_node);
     
    if(para_node!=NULL)
    {
    }
     // add plugin dir in lib_para's dynamic lib
/*
    if(plugin_dir!=NULL)
    {
	Strncpy(filename,plugin_dir,DIGEST_SIZE*4);
	Strcat(filename,"/");
	Strncat(filename,lib_para->dynamic_lib,DIGEST_SIZE*4);
	lib_para->dynamic_lib=Salloc0(Strlen(filename)+1);
	Strcpy(lib_para->dynamic_lib,filename);
    }
*/		     	
    return ret;
}

int read_main_cfg(void * lib_para_struct,void * root_node)
{
    int ret;
    void * temp_node;
    int (*init) (void *,void *);
    temp_node=json_find_elem("proc_name",root_node);
    if(temp_node==NULL)
	return -CUBEERR_INVAL;
    
    void * init_para;
    	
    ret=proc_share_data_setvalue("proc_name",json_get_valuestr(temp_node));
    if(ret<0)
	return ret;

	return 0;	
    ret=0;

    void * record_list=json_find_elem("record_file",root_node);
    if(record_list!=NULL)
    {
	if(json_get_type(record_list)==JSON_ELEM_STRING)
	{
		ret=read_record_file(json_get_valuestr(record_list));
		if(ret>0)
			print_cubeaudit("read %d elem from file %s!",ret,json_get_valuestr(record_list));
	}
	else if(json_get_type(record_list)==JSON_ELEM_ARRAY)
	{
		void * record_file=json_get_first_child(record_list);
		while(record_file!=NULL)
		{
			ret=read_record_file(json_get_valuestr(record_file));
			if(ret>0)
				print_cubeaudit("read %d elem from file %s!",ret,json_get_valuestr(record_file));
			else
				print_cubewarn("read record file %s failed!",json_get_valuestr(record_file));
			record_file=json_get_next_child(record_list);
		}
	}	
    }		

	temp_node=json_find_elem("init_para",root_node);
    if(temp_node!=NULL)
        {
	}
	ret=ex_module_init(NULL,init_para);
    return ret;
}
